import { defineStore } from "pinia";
import { cartUsersService } from "@/services/cartUsersService";
import { ElMessage } from 'element-plus'
import type { List } from '../types/common'
import type { SearchParams, cartUsersItem } from '../types/cartUsers'

export const cartUsersStore = defineStore('cartUsersStore', {
    state: () => ({
        usersData: {} as List<cartUsersItem>
    }),

    actions: {
        /**  新增 */
        async createCartUsers(params: cartUsersItem) {
            const { code } = await cartUsersService.createCartUsersService(params)

            ElMessage({
                // message: `添加${message}`,
                message: `添加${code ? '失败' : '成功'}`,
                type: code ? 'warning' : 'success',
            })

            return { code }
        },

        /**  删除 */
        async deleteCartUsers(params: { userId: string | number }) {
            const { code, message } = await cartUsersService.deleteCartUsersService(params)

            ElMessage({
                // message: `添加${message}`,
                message: `删除${message}`,
                type: code ? 'warning' : 'success',
            })

            return { code }

        },

        /**  修改 */
        async updateCartUsers(params: cartUsersItem) {
            const { code, message } = await cartUsersService.updateCartUsersService(params)

            ElMessage({
                // message: `添加${message}`,
                message: `修改${message}`,
                type: code ? 'warning' : 'success',
            })

            return { code }
        },

        /**  查询 */
        async getCartUsers(params: SearchParams) {
            this.usersData = {}  as List<cartUsersItem>

            const { code, message, data } = await cartUsersService.getCartUsersService(params)

            if (code) {
                ElMessage({
                    message: message,
                    type: code ? 'warning' : 'success',
                })
            } else {
                this.usersData = data
            }
        }

    }
})